package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class AccountFlowStageRoleController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AccountFlowStageRole.list(params), model:[accountFlowStageRoleCount: AccountFlowStageRole.count()]
    }

    def show(AccountFlowStageRole accountFlowStageRole) {
        respond accountFlowStageRole
    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        respond new AccountFlowStageRole(params)
    }

    @Transactional
    def save(AccountFlowStageRole accountFlowStageRole) {
        if (accountFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountFlowStageRole.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountFlowStageRole.errors, view:'create'
            return
        }

        accountFlowStageRole.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'accountFlowStageRole.label', default: 'AccountFlowStageRole'), accountFlowStageRole.id])
                redirect url: params['targetUri']
            }
            '*' { respond accountFlowStageRole, [status: CREATED] }
        }
    }

    def edit(AccountFlowStageRole accountFlowStageRole) 
    {
        params['targetUri'] = request.getHeader("referer")
        respond accountFlowStageRole
    }

    @Transactional
    def update(AccountFlowStageRole accountFlowStageRole) 
    {
        if (accountFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountFlowStageRole.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountFlowStageRole.errors, view:'edit'
            return
        }

        accountFlowStageRole.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountFlowStageRole.label', default: 'AccountFlowStageRole'), accountFlowStageRole.id])
                redirect url: params['targetUri']
            }
            '*'{ respond accountFlowStageRole, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountFlowStageRole accountFlowStageRole) {

        if (accountFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountFlowStageRole.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountFlowStageRole.label', default: 'AccountFlowStageRole'), accountFlowStageRole.id])
                redirect url: request.getHeader("referer")
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountFlowStageRole.label', default: 'AccountFlowStageRole'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
